import type { MenuProps } from 'antd'
import { Dropdown, Space } from 'antd'
import React from 'react'
import type { RenderWidgetBaseProps } from '../interface'
import { tryJSONParse } from '../utils'
import styles from './style/index.module.css'
import { useChatContext } from '@/app/components/chat/chat-context'
import Toast from '@/app/components/base/toast'
import { IconLocate } from '../icons'

type PosData = {
  x: number
  y: number
  address: string
}
export const ListItem: React.FC<RenderWidgetBaseProps> = ({
  children,
  config,
}) => {
  const { sendText } = useChatContext()
  const handleClick: MenuProps['onClick'] = (item) => {
    const posData = tryJSONParse<PosData>(String(children))
    if (!posData) {
      Toast.notify({
        type: 'info',
        message: '无效的地址数据！',
      })
      return
    }
    console.log('LP:', config, posData)
    if (item.key === 'BGFX')
      sendText(`帮我对'${posData.address}'进行爆管分析 {{hidden: (x:${posData.x}, y: ${posData.y})}}`)
  }
  const items: MenuProps['items'] = [
    {
      key: 'BGFX',
      label: '对当前位置进行爆管分析',
    },
  ]
  return (
    <span data-role={config.role}>
      <Dropdown menu={{
        items,
        onClick: handleClick,
      }}
      >
        <a onClick={e => e.preventDefault()}>
          <Space>
            <span className={styles.icon}><IconLocate/></span>
          </Space>
        </a>
      </Dropdown>

    </span>
  )
}
